import rules from "./rules/rules.js";

// 触摸事件和手势的跨浏览器支持
// import Hammer from "hammerjs"

import cornerstone from "@cornerstonejs/core/dist/cjs/index"
// 某些工具用于辅助向量数学或其他复杂运算的依赖项
import cornerstoneMath from "cornerstone-math"
import cornerstoneTools from "@cornerstonejs/tools"
// 对DICOM医学影像文件的支持。
import cornerstoneWADOImageLoader from 'cornerstone-wado-image-loader';
import dicomParser from '../node_modules/dicom-parser/dist/dicomParser.min.js';
// web图片加载
import cornerstoneWebImageLoader from "cornerstone-web-image-loader"
// cornerstoneWebImageLoader.external.cornerstone = cornerstone
// 注册并挂载cornerstone及其cornerstoneTools，固定操作
cornerstoneTools.external.cornerstone = cornerstone;
cornerstoneTools.external.cornerstoneMath = cornerstoneMath;
// cornerstoneTools.external.Hammer = Hammer;
cornerstoneWADOImageLoader.external.dicomParser = dicomParser;
cornerstoneWADOImageLoader.external.cornerstone = cornerstone;
import axios from 'axios';


export default {
    components: {

    },
    data() {
        return {
            rules,
            labelPosition: 'left',
            formLabelAlign: {
                weizhi: '',
                name: '',
                region: '',
                type: ''
            },
            disabled: true,
            // FOV
            fovfrom: {
                type: '0',
            },

            // 曝光操作
            exposurefrom: {
                modeType: '0',
                pageNumber: '',
            },
            modeTydisabled: false,
            dialogVisible: false,//是否显示编辑球管参数弹窗
            islogin: false,//是否显示登录弹窗
            loginfrom: {
                username: '',
                password: '',
            },
            parameterList: [
                {
                    name: '窗宽：',//名称
                    displacementvalue: 0,//位移值
                },
                {
                    name: '窗位：',
                    displacementvalue: 0,//位移值
                },

            ],
            myChart: null,
            option: {
                title: {
                    text: '75%',
                    x: 'center',
                    y: 'center',
                    textStyle: {
                        fontWeight: 'normal',
                        color: "red",
                        fontSize: 15
                    }
                },
                backgroundColor: '#D7D7D7',
                color: ['#009DFF', '#D7D7D7',],
                tooltip: {
                    show: false,
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                legend: {
                    show: false,
                    itemGap: 12,
                    data: ['01', '02']
                },
                toolbox: {
                    show: false,
                    feature: {
                        mark: {
                            show: true
                        },
                        dataView: {
                            show: true,
                            readOnly: false
                        },
                        restore: {
                            show: true
                        },
                        saveAsImage: {
                            show: true
                        }
                    }
                },
                series: [
                    {
                        name: 'Line 1',
                        type: 'pie',
                        clockWise: false,
                        radius: [100, 40],
                        itemStyle: {
                            normal: {
                                label: {
                                    show: false
                                },
                                labelLine: {
                                    show: false
                                },
                                shadowBlur: 0,
                                shadowColor: 'rgba(40, 40, 40, 0.5)',
                            }
                        },
                        hoverAnimation: false,

                        data: [{
                            value: 90,
                            name: '01'
                        }, {
                            value: 10,
                            name: 'invisible',
                            itemStyle: {
                                normal: {
                                    color: 'rgba(0,0,0,0)',
                                    label: {
                                        show: false
                                    },
                                    labelLine: {
                                        show: false
                                    }
                                },
                                emphasis: {
                                    color: 'rgba(0,0,0,0)'
                                }
                            }
                        }

                        ]
                    },
                    {
                        name: 'Line 2',
                        type: 'pie',
                        animation: false,
                        clockWise: false,
                        radius: [50, 100],
                        itemStyle: {
                            normal: {
                                label: {
                                    show: false
                                },
                                labelLine: {
                                    show: false
                                },
                                shadowBlur: 0,
                                shadowColor: 'rgba(40, 40, 40, 0.5)',
                            }
                        },
                        hoverAnimation: false,
                        tooltip: {
                            show: false
                        },
                        data: [{
                            value: 100,
                            name: '02',
                            itemStyle: {
                                emphasis: {
                                    color: '#313443'
                                }
                            }
                            // }, {
                            //     value: 0,
                            //     name: 'invisible',
                            //     itemStyle: {
                            //         normal: {
                            //             color: 'rgba(0,0,0,0)',
                            //             label: {
                            //                 show: false
                            //             },
                            //             labelLine: {
                            //                 show: false
                            //             }
                            //         },
                            //         emphasis: {
                            //             color: 'rgba(0,0,0,0)'
                            //         }
                            //     }
                        }

                        ]
                    },


                ]
            },
            dicomUrl: '../src/assets/TG119_DICOM/0887/CT1.2.840.113619.2.55.3.279721844.297.1204122294.826.10.dcm',
            renderingEngine: null,
            ele: null,
            imageId: '',
            url: '',
            files: '',
            imageIds: [],
            dicomList: [],//dcm的list
            responseData: {},//列表数据

        };
    },
    computed: {},
    created() {},
    mounted() {
        this.choicFun()
        this.mycharts()
        this.getImagefun()
        this.showimage()
         cornerstone.imageLoader.registerImageLoader("myCustomLoader", this.loadImage);
    },
    beforeUpdate() {},
    beforeDestroy() {},
    methods: {
        loadImage(imageId) {
            return console.log(imageId, 'imageIdimageIdimageId')
            // 图像注册
            const url = 'http://192.168.1.26:8010/oring/v1/image/GetImage?patientId=202239'//parseImageId();

            const promise = new Promise((resolve, reject) => {
                // 生成dicom请求数据

                const oReq = new XMLHttpRequest();
                oReq.open("get", url, true);
                oReq.responseType = "arraybuffer";
                oReq.onreadystatechange = function (oEvent) {
                    if (oReq.readyState === 4) {
                        if (oReq.status == 200) {

                            // 请求成功后，创建图像对象
                            // const image = createImageObject(oReq.response);

                            resolve(image);
                        } else {
                            reject(new Error(oReq.statusText));
                        }
                    }
                };

                oReq.send();
            });

            return {
                promise,
            };
        },


        //   列表接口
        getImagefun() {

            const oReq = new XMLHttpRequest();
            const url = 'http://192.168.1.26:8010/oring/v1/image/GetImage?patientId=202239';
            oReq.open('GET', url, true);
            oReq.setRequestHeader('Content-Type', ' application/json')
            // 成功回调
            oReq.onload = () => {

                if (oReq.status === 200) {
                    console.log(oReq, 'oReqoReqoReqoReqoReq')
                    this.responseData = JSON.parse(oReq.response)
                    console.log(this.responseData.data, 'response');
                    this.responseData.data.reconstructedImages.forEach((item, index) => {
                        this.dicomList.push('myCustomLoader:' + item)
                       
                    })
                    console.log(this.dicomList, 'dicomList')



                    // this.dicomList=['myCustomLoader://'+``]
                    // ['myCustomLoader://example.com/image.dcm', 'myCustomLoader://example.com/image.dcm']
                } else {
                    console.error('Request failed.  Returned status of ' + oReq.status);
                }
            };
            //   失败回调
            oReq.onerror = () => {
                console.error('XMLHttpRequest error.');
            };

            // var data = { "path": "starage\\uploads\\dicom_output6\\1.dcm" }; // 准备要发送的数据对象  
            // var params = JSON.stringify(data); // 将数据对象转换为JSON字符串  
            oReq.send(); // 发送请求和参数
        },
        // 展示图像
        showimage() {

            var element = document.getElementById('cornerstone');
           console.log(element,'element')
            const renderingEngineId = 'myRenderingEngine';
            const viewportId = 'CT_AXIAL_STACK';
            // cornerstone.rendering.RenderingEngine
            // cornerstone.
            const renderingEngine = new RenderingEngine(renderingEngineId);
            console.log(renderingEngine,'renderingEngine')
            const viewportInput = {
                viewportId,
                element,
                type: ViewportType.STACK,
            };

            renderingEngine.enableElement(viewportInput);

            const viewport = renderingEngine.getViewport(viewportInput.viewportId);
            viewport.setStack(this.dicomList, 60);

            viewport.render();


        },

        // 机架运动
        gantrymovement(ruleForm) {
            this.$refs[ruleForm].validate((valid) => {
                if (valid) {
                    console.log(valid, 'valid')
                } else {
                    console.log(valid, 'valid1111')
                    return false;
                }
            });
        },
        // 执行机架偏转
        rackdeflection(rackdeflection) {
            this.$refs[rackdeflection].validate((valid) => {
                if (valid) {
                    console.log(valid, 'valid')
                } else {
                    console.log(valid, 'valid1111')
                    return false;
                }
            });
        },
        //平板偏移
        plateoffset() { },
        // 激光1  type：1激光1    依次排序
        laserBtn(type) { },
        // 曝光准备
        exposurepreparation() { },
        // 曝光环形图
        // 仪表图
        mycharts() {
            // 绘制图表
            this.myChart = this.$echarts.init(document.getElementById("myechart"));
            //初始化配置项
            this.myChart.setOption(this.option);
            window.addEventListener("resize", () => {
                // 自动渲染echarts
                if (this.myChart) {
                    this.myChart.resize();
                }
            });
        },
        modeTypecchange() {
            this.choicFun()
        },
        // 曝光操作模式选择判断
        choicFun() {
            if (this.exposurefrom.modeType == 0) {
                this.exposurefrom.pageNumber = ''
                this.modeTydisabled = true
            } else {
                this.exposurefrom.pageNumber = '0'
                this.modeTydisabled = false
            }
        },
        // 修改默认参数按钮
        editdefaultBtn() {
            this.dialogVisible = true
        },
        // 
        handleClose(done) {
            this.dialogVisible = false

        },
        // 登录弹窗确认按钮
        submitFormNew(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.islogin = false
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },

    },




}



